/**
 * 用于(项目/产品)各自单独自定义的js工具类
 **/
var mytools = {};

/**
 * 清空数据，按照样式清空；
 * 1）按照form表单Id清空：id参数#开头
 * 2）按照form表单class样式清空：id参数.开头
 */
mytools.cleanData=function(id){
	$(id).find(".clean").val("");
	$(id).find("IMG").each(function(){
		$(this).attr("src",strUtil.doNull($(this).attr("data-def"), ctx+"/images/def.jpg"));
		//$(this).next().text("选择主图");
		$(this).siblings("label").eq(2).text("");
		$(this).siblings("a").text("");
	});
}

/**
 * 清空校验规则提示信息，按照样式清空；
 * 1）按照form表单Id清空：id参数#开头
 * 2）按照form表单class样式清空：id参数.开头
 */
mytools.cleanValidate=function(id){
	var validate = $(id).data('bootstrapValidator').validate();
	validate.resetForm();
}
/**
 * 关闭模态窗口
 */
mytools.closeModal=function(id){
	$('#'+id).modal('hide');
}
/**
 * 封装的pjax跳转方法1
 */
mytools.pjax=function(url,container,fragment){
	var defcon = ".content-wrapper";
	var a=defcon;
	var b=defcon;
	if(container!=undefined &&container!=null && container!=''){
		a = container;
	}
	if(fragment!=undefined &&fragment!=null && fragment!=''){
		b = fragment;
	}
	//alert(url+"  "+a+"  "+b);
	 $.pjax({  
         url: url,  
         container: a,  
         fragment: b,
         cache: false,
         //maxCacheLength:0,
         timeout: 8000
       })
}

/**
 * 封装的load跳转方法 常用按钮跳转
 */
mytools.load=function(url){
	var histClick = clickMenu;
	$.ajaxSetup ({cache: false});
	$(".content-wrapper").load(url+" .content-wrapper>",function(resultData){
		 $result = $(resultData);
        $result.find("script").appendTo(".content-wrapper");//保证子页面的js内容能被引入进来
        clickMenu = histClick;
        showClickMenu();
	});
}


/**
 * 站点以端口方式访问时，需要替换top连接地址
 * @param thisObj
 * @param findStr
 * @param newStr
 */
mytools.portRequestReplaceLink=function(thisObj) {
    if(window.location.port){
        //  console.log("请求方式：","因IP+端口方式，需top.html的a标签href地址内["+findStr+"] 替换为 ["+newStr+"]");
		var newStr = '/wcm/'+siteCode+'/';
		var findStr = '../../../';
        $("[data-repla-port-link='true']").find("a[href^='"+findStr+"']").each(function () {
            var regExpB=new RegExp(findStr,"g");
            $(this).attr("href",$(this).attr("href").replace(regExpB,newStr));
            // $(this).attr("href",$(this).attr("href").replace(/\..\/..\//g,"\/wcm\/ttwl\/"));
            // console.log($(this).text()+"-new-->",$(this).attr("href"));
        });
        $("[data-repla-port-link='true']").find("img[src^='"+findStr+"']").each(function () {
            var regExpB=new RegExp(findStr,"g");
            // console.log("old-->",$(this).attr("src"));
            $(this).attr("src",$(this).attr("src").replace(regExpB,newStr));
            // console.log("new-->",$(this).attr("src"));
        });
    }
}
$(function(){
    $("[data-temp-code]").each(function(){
        var thisObj = $(this);
        var objId = thisObj.attr("id");
        var code = thisObj.attr("data-temp-code");
        //console.log("加载["+siteCode+"]的["+code+"]栏目数据...");
        var tempType = thisObj.attr("data-temp-type");
        var dataTempRows = doNull(thisObj.attr("data-temp-rows"),5);
        var dataTempHeight = doNull(thisObj.attr("data-temp-height"),"200px");//默认高度
        var dataDefVal = doNull(thisObj.attr("data-temp-def"),"/images/def6.jpg");
        var dataTempCols = doNull(thisObj.attr("data-temp-cols"),"12");

        //alert(objId+"|"+code+"|"+tempType+"|"+dataTempRows);

        var tempId = objId;
        if(!isNull(tempType)){
            tempId = tempType+"-template";
        }else{
            alert(objId+"缺少data-temp-type元素");
            return;
        }
        if("banner"==tempType || "bannerTitle"==tempType){
            //tempType=banner单独处理
            tempId = tempType+"-template";
            var objMap = {};
            objMap.objId=objId;
            objMap.code=code;
            objMap.tempId=tempId;
            objMap.dataTempRows=dataTempRows;
            objMap.dataTempHeight=dataTempHeight;
            objMap.dataDefVal = dataDefVal;
            objMap.dataTempCols = dataTempCols;
            loadBannerBox(objMap);
        }else if("thisId"==tempType){
            //tempType=data
            tempId = objId+"-template";
            $.getJSON("../data/"+code+".json",function(myData){
                myData.siteCode=siteCode;
                myData.dataTempRows=dataTempRows;
                myData.dataTempHeight=dataTempHeight;
                myData.dataDefVal = dataDefVal;
                myData.dataTempCols = dataTempCols;
                var htmlContent = baidu.template(tempId,eval(myData));
                $("#"+objId).html(htmlContent);
            });
        }else{
            $.ajaxSetup ({cache: false});
            $.getJSON("../data/"+code+".json",function(myData){
                myData.siteCode=siteCode;
                myData.dataTempRows=dataTempRows;
                myData.dataTempHeight=dataTempHeight;
                myData.dataDefVal = dataDefVal;
                myData.dataTempCols = dataTempCols;
                var htmlContent = baidu.template(tempId,eval(myData));
                $("#"+objId).html(htmlContent);
            });
        }
    });

    $("[data-repla-port-link='true']").each(function () {
        mytools.portRequestReplaceLink($(this));
    });

});

//加载CMS发布的栏目数据，作为Banner images
function loadBannerBox(paramMap){
    $.getJSON("../data/"+paramMap["code"]+".json",function(myData){
        console.log(myData);
        //解析数据
        myData.siteCode=siteCode;
        myData.dataTempRows=paramMap["dataTempRows"];
        myData.dataTempHeight=paramMap["dataTempHeight"];
        myData.dataDefVal = paramMap["dataDefVal"];
        myData.dataTempCols = paramMap["dataTempCols"];
        var dataHtml = baidu.template(paramMap["tempId"],eval(myData));
        var objId = paramMap["objId"];
        $("#"+objId).html(dataHtml);
        //初始化Banner 的滚屏插件 fullSlide
        $("#"+objId).find(".fullSlide").hover(function(){
                $(this).find(".prev,.next").stop(true, true).fadeTo("show", 0.5)
            },
            function(){
                $(this).find(".prev,.next").fadeOut()
            });
        $("#"+objId).find(".fullSlide").slide({
            titCell: ".hd ul",
            mainCell: ".bd ul",
            effect: "fold",
            autoPlay: true,
            autoPage: true,
            trigger: "click",
            startFun: function(i) {
                var curLi = jQuery(".fullSlide .bd li").eq(i);
                if ( !! curLi.attr("_src")) {
                    curLi.css("background-image", curLi.attr("_src")).removeAttr("_src")
                }
            }
        });
    });
}

function isNull(str){
    if(null==str||undefined==str){
        return true;
    }
    if(""==str||"null"==str||"undefined"==str||JSON.stringify(str).trim().length==0){
        return true;
    }
    return false;
}
function doNull(str, defVal){
    if(isNull(str)){
        return defVal;
    }
    return str;
}

